package swing;

import app.Main;
import packages.db.DBUtil;
import user.UserInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

import static app.Main.viewChange;

public class LoginView extends JPanel{
    public JPanel JPanel0;
    private JPanel JPanel1;
    private JPanel JPanel2;
    private JPanel JPanel3;

    private JButton ReturnButton;
    private JButton RegisterButton;

    private JLabel NameLabel;
    private JLabel PasswordLabel1;
    private JLabel TitleLabel;

    private JTextField NameField;
    private JPasswordField PasswordField1;

    String username;
    String password;


    public LoginView() throws IOException {
        Font font=new Font("楷体", Font.BOLD,35);
        TitleLabel.setFont(font);
        font=new Font("楷体", Font.BOLD,20);
        NameLabel.setFont(font);
        PasswordLabel1.setFont(font);
        RegisterButton.setFont(font);
        ReturnButton.setFont(font);
//        Color color = new Color(0xff, 0xf5, 0xee);
        Color color = new Color(0xf7, 0xf7, 0xf7);
        ReturnButton.setBackground(color);
        RegisterButton.setBackground(color);

        JPanel0.setOpaque(false);
        JPanel1.setOpaque(false);
        JPanel2.setOpaque(false);
        JPanel3.setOpaque(false);

        RegisterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 得到所有字段
                username = NameField.getText();
                password = new String(PasswordField1.getPassword());

                // 检查格式
                if(!userNameMatch(username)){
                    // 用户名格式错误
                    JOptionPane.showMessageDialog(null,"注册失败!\n用户名或密码错误!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
                else if(!passwordMatch(password)){
                    // 密码格式错误
                    JOptionPane.showMessageDialog(null,"注册失败!\n用户名或密码错误!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }else {
                    // 格式正确

                    //查询本地数据库
                    DBUtil db = new DBUtil();

                    //更新操作（增加数据）
                    Object[] objs = {};
                    username = "'" + username + "'";
                    password = "'" + password + "'";
                    ResultSet set = db.select("select * from user where password = " + password + "and username = " + username, objs);
                    int res = 0;
                    try {
                        if (set.next()) {
                            // 获取全局用户信息
                            UserInfo userInfo = UserInfo.getInstance();
                            userInfo.setUserId(set.getInt("user_id"));
                            userInfo.setUsername(set.getString("username"));

                            // 弹出提示框
                            JOptionPane.showMessageDialog(null,
                                    "欢迎你，" + userInfo.getUsername() + "\n让我们一起呵护校园的猫猫~",
                                    "平台提示",
                                    JOptionPane.PLAIN_MESSAGE);
                            NameField.setText("");
                            PasswordField1.setText("");

                            // 页面跳转
                            viewChange = 3;
                            synchronized (Main.object){
                                Main.object.notify();
                            }
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"登录失败!\n用户名或密码错误!","提示",
                                    JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (SQLException sqle) {
                        sqle.printStackTrace();
                    }
                    db.closeConnection();

                }
            }
        });
        ReturnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewChange = 0;
                synchronized (Main.object){
                    Main.object.notify();
                }
            }
        });
    }

    // 用于匹配用户名是否符合规范 5-15位 以字母开头且只包含字母 数字 _
    private boolean userNameMatch(String userName){
        String pattern = "^[a-zA-Z][a-zA-Z0-9_-]{4,14}$";
        return (Pattern.matches(pattern, userName));
    }

    // 用于匹配密码是否符合规范 5-15位 只包含字母 数字 - _ , . / @ #
    private boolean passwordMatch(String password){
        String pattern = "^[a-zA-Z0-9_,./@#-]{5,15}$";
        return (Pattern.matches(pattern, password));
    }
}
